home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Printf.c
-
- Contains: A dcmd that supports the C concept of printf.
-
- Written by: JM3 = Jim Murphy
- sad = Scott Douglass
-
- Copyright: © 1989, 1994-1995 by Apple Computer, Inc., All Rights Reserved.
-
- Change History (most recent first):
-
- <3> 3/26/95 DAL Included <:Public:Strings.h> to avoid p2cstr warning (should fix
- header file names). Used new dcmdFillVersion and dcmdFillString
- macros.
- <2> 10-Dec-94 JM3 Updated for new format 3 dcmd requirements.
- <1> 2/16/89 sad written
-
- */
-
- /* This is mostly to show how dcmds can use the standard C library.
-
- The following MPW commands will build the dcmd and copy it to the
- "Debugger Prefs" file in the System folder. The dcmd's name in
- MacsBug will be the name of the file built by the Linker.
- You must first copy dcmd.h, dcmdGlue.a.o and DRunTime.o from the
- C Samples folder into this folder.
-
- C Put.c
- C Printf.c
- Link dcmdGlue.a.o Printf.c.o put.c.o -d DRuntime.o "{Libraries}"Interface.o ∂
- "{CLibraries}StdCLib.o" "{CLibraries}CInterface.o" -sg Main=STDIO -o Printf
- BuildDcmd Printf 1004
- Echo 'include "Printf";' | Rez -a -o "{systemFolder}Debugger Prefs"
- */
-
- #include <Types.h>
-
- #include <stdio.h>
- #include <string.h>
- #include <:Public:Strings.h>
- #include <Memory.h>
-
- #include "dcmd.h"
- #include "put.h"
-
- pascal void CommandEntry(dcmdBlock* paramPtr)
- {
-
- static const char usageStr[] = "\p\"format\" arg...";
-
- switch (paramPtr->request)
- {
- case dcmdInit:
- break;
-
- case dcmdHelp:
- dcmdDrawLine("\pDisplays the arguments according to the format (no floating point).");
- break;
-
- case dcmdGetInfo:
- dcmdFillVersion(paramPtr, 0x03008000); // version 3.0
- dcmdFillString(paramPtr, usageStr, usageStr);
- break;
-
- case dcmdDoIt:
- {
- Str255 formatstring;
- unsigned char* formatp = formatstring;
- Str255 formattedstring;
-
- dcmdSwapWorlds(); // not really necessary because we don’t access machine state
-
- (void)dcmdGetNextParameter(formatstring);
-
- p2cstr(formatstring);
-
- while (*formatp != '\0')
- {
- char aformatspec[256];
- size_t speclength;
- long arg;
- Boolean ok;
-
- // Find the next format spec (i.e. %d) and print out anything before it
-
- char* nextp = strchr(formatp, '%');
- if (nextp == nil) nextp = formatp + strlen(formatp);
-
- PutBytesTo(formatp, nextp - formatp, 0);
- formatp = nextp;
-
- // Find the length of the format spec
-
- speclength = strcspn(formatp + 1, "diouxXcspP%") + 2; // we don’t do "feEgGn"
- memcpy(aformatspec, formatp, speclength);
- aformatspec[speclength] = '\0';
- (void)dcmdGetNextExpression(&arg, &ok);
- if (!ok) break;
-
- sprintf(formattedstring, aformatspec, arg);
- PutCStr(formattedstring);
- formatp = formatp + speclength;
- }
- PutLine();
-
- dcmdSwapWorlds();
- }
- break;
-
- // Version 3 and newer dcmds must quietly ignore requests we don't recognize.
-
- default:
- break;
- }
-
- } // CommandEntry
-
- /*
- the following are stubs to override the C library routines so that the dcmd isn’t
- so big.
- */
-
- size_t fwrite (const void *, size_t, size_t, FILE *) { return 0; } // wont’t actually be called by sprintf
- _flsbuf() {} // wont’t actually be called by sprintf
-
- fcvt() {} // used only for floating point %f, etc.
- ecvt() {} // used only for floating point %e, etc.
-